//
//  RadiateChartView.m
//  JunZeWristband
//
//  Created by Kendama on 2017/9/12.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "RadiateChartView.h"

@implementation RadiateChartView

//- (BarChartData *)setData{
//    
//    int xVals_count = 12;//X轴上要显示多少条数据
//    double maxYVal = 100;//Y轴的最大值
//    
//    //X轴上面需要显示的数据
//    NSMutableArray *xVals = [[NSMutableArray alloc] init];
//    for (int i = 0; i < xVals_count; i++) {
//        [xVals addObject:[NSString stringWithFormat:@"%d月", i+1]];
//    }
//    
//    //对应Y轴上面需要显示的数据
//    NSMutableArray *yVals = [[NSMutableArray alloc] init];
//    for (int i = 0; i < xVals_count; i++) {
//        double mult = maxYVal + 1;
//        double val = (double)(arc4random_uniform(mult));
//        BarChartDataEntry *entry = [[BarChartDataEntry alloc] initWithValue:val xIndex:i];
//        [yVals addObject:entry];
//    }
//
//    //创建BarChartDataSet对象，其中包含有Y轴数据信息，以及可以设置柱形样式
//    BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:nil];
//    set1.barSpace = 0.2;//柱形之间的间隙占整个柱形(柱形+间隙)的比例
//    set1.drawValuesEnabled = YES;//是否在柱形图上面显示数值
//    set1.highlightEnabled = NO;//点击选中柱形图是否有高亮效果，（双击空白处取消选中）
//    [set1 setColors:ChartColorTemplates.material];//设置柱形图颜色
//    //将BarChartDataSet对象放入数组中
//    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
//    [dataSets addObject:set1];
//    
//    //创建BarChartData对象, 此对象就是barChartView需要最终数据对象
//    BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
//    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];//文字字体
//    [data setValueTextColor:[UIColor orangeColor]];//文字颜色
//    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
//    //自定义数据显示格式
//    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
//    [formatter setPositiveFormat:@"#0.0"];
//    [data setValueFormatter:formatter];
//    
//    return data;
//}

- (void)updateChartData
{
//    if (self.shouldHideData)
//    {
//        _chartView.data = nil;
//        return;
//    }
    
//    [self setDataCount:13 + 1 range:12];
}

//- (void)setDataCount:(int)count range:(double)range
//{
//    double start = 1.0;
//    
//    NSMutableArray *yVals = [[NSMutableArray alloc] init];
//    
//    for (int i = start; i < start + count + 1; i++)
//    {
//        double mult = (range + 1);
//        double val = (double) (arc4random_uniform(mult));
//        if (arc4random_uniform(100) < 25) {
//            [yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:val icon: [UIImage imageNamed:@"icon"]]];
//        } else {
//            [yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:val]];
//        }
//    }
//    
//    BarChartDataSet *set1 = nil;
//    if (_chartView.data.dataSetCount > 0)
//    {
//        set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
//        set1.values = yVals;
//        [_chartView.data notifyDataChanged];
//        [_chartView notifyDataSetChanged];
//    }
//    else
//    {
//        set1 = [[BarChartDataSet alloc] initWithValues:yVals label:@""];
//        [set1 setColors:ChartColorTemplates.material];
//        set1.drawIconsEnabled = NO;
//        
//        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
//        [dataSets addObject:set1];
//        
//        BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
//        [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
//        
//        data.barWidth = 0.9f;
//        
//        _chartView.data = data;
//    }
//}
- (void)setData:(NSArray *)data
{
    double start = 0;
    
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < data.count; i++) {
        [xVals addObject:[NSString stringWithFormat:@"%d", i+1]];
    }
    
    ChartXAxis *xAxis = _chartView.xAxis;
    xAxis.axisLineWidth = 1;//设置X轴线宽
    xAxis.labelPosition = XAxisLabelPositionBottom;//X轴的显示位置，默认是显示在上面的
    xAxis.drawGridLinesEnabled = NO;//不绘制网格线
    xAxis.axisMinimum = 0;
//    xAxis.spaceBetweenLabels = 4;//设置label间隔，若设置为1，则如果能全部显示，则每个柱形下面都会显示label

//    xAxis.labelPosition = XAxisLabelPositionBottom;
//    xAxis.labelFont = [UIFont systemFontOfSize:10.f];
//    xAxis.drawGridLinesEnabled = NO;
//    xAxis.granularity = 1.0; // only intervals of 1 day
//    xAxis.labelCount = 7;
    if (data.count == 8)
    {
        xAxis.spaceMax = 1;
        xAxis.valueFormatter = [[DayAxisValueFormatter alloc] initForChart:_chartView];
        xAxis.labelCount = 7;
    }
    else if (data.count > 8)
    {
        xAxis.spaceMax = 4;
        xAxis.spaceMin = 1;
        xAxis.valueFormatter = nil;
        xAxis.labelCount = 10;
    }
    
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    
    for (int i = start; i < data.count; i++)
    {
        [yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:[data[i] floatValue]]];
    }
    
    BarChartDataSet *set1 = nil;
//    if (_chartView.data.dataSetCount > 0)
//    {
//        set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
//        set1.values = yVals;
//        [_chartView.data notifyDataChanged];
//        [_chartView notifyDataSetChanged];
//    }
//    else
//    {
        set1 = [[BarChartDataSet alloc] initWithValues:yVals label:@""];
        [set1 setColors:@[colorWithAlpha(248, 165, 165, 1)]];
        set1.drawIconsEnabled = NO;
        set1.drawValuesEnabled = NO;//是否在柱形图上面显示数值

        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        
        BarChartData *barData = [[BarChartData alloc] initWithDataSets:dataSets];
//        BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
        [barData setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
        
        barData.barWidth = 0.9f;
        
        _chartView.data = barData;
//    }
}

- (BarChartView *)chartView
{
    if(!_chartView)
    {
        UIView *superView = self;
        _chartView = [[BarChartView alloc]init];
        [superView addSubview:_chartView];
//        _chartView.backgroundColor = [UIColor colorWithRed:230/255.0f green:253/255.0f blue:253/255.0f alpha:1];
        _chartView.noDataText = MyNSLocalizedString(@"暂无数据");//没有数据时的文字提示
        _chartView.drawValueAboveBarEnabled = YES;//数值显示在柱形的上面还是下面
////        _chartView.drawHighlightArrowEnabled = NO;//点击柱形图是否显示箭头
//        _chartView.drawBarShadowEnabled = NO;//是否绘制柱形的阴影背景
//        _chartView.scaleYEnabled = NO;//取消Y轴缩放
//        _chartView.doubleTapToZoomEnabled = NO;//取消双击缩放
//        _chartView.dragEnabled = YES;//启用拖拽图表
//        _chartView.dragDecelerationEnabled = YES;//拖拽后是否有惯性效果
//        _chartView.dragDecelerationFrictionCoef = 0.9;//拖拽后惯性效果的摩擦系数(0~1)
//        
//        ChartXAxis *xAxis = _chartView.xAxis;
//        xAxis.axisLineWidth = 1;//设置X轴线宽
//        xAxis.labelPosition = XAxisLabelPositionBottom;//X轴的显示位置，默认是显示在上面的
//        xAxis.drawGridLinesEnabled = NO;//不绘制网格线
////        xAxis.spaceBetweenLabels = 4;//设置label间隔，若设置为1，则如果能全部显示，则每个柱形下面都会显示label
//        xAxis.labelTextColor = [UIColor brownColor];//label文字颜色
//        
//        _chartView.rightAxis.enabled = NO;//不绘制右边轴
        _chartView.legend.enabled = NO;
        _chartView.descriptionText = @"";
        _chartView.scaleYEnabled = NO;//取消Y轴缩放
        _chartView.scaleXEnabled = NO;//取消Y轴缩放
        _chartView.doubleTapToZoomEnabled = NO;//取消双击缩放
        
//        ChartXAxis *xAxis = _chartView.xAxis;
//        xAxis.labelPosition = XAxisLabelPositionBottom;
//        xAxis.labelFont = [UIFont systemFontOfSize:10.f];
//        xAxis.drawGridLinesEnabled = NO;
//        xAxis.granularity = 1.0; // only intervals of 1 day
//        xAxis.labelCount = 7;
////        xAxis.spaceMin = 1;
//        xAxis.spaceMax = 4;
//        xAxis.valueFormatter = [[DayAxisValueFormatter alloc] initForChart:_chartView];
        
        NSNumberFormatter *leftAxisFormatter = [[NSNumberFormatter alloc] init];
        leftAxisFormatter.minimumFractionDigits = 0;
        leftAxisFormatter.maximumFractionDigits = 1;
//        leftAxisFormatter.negativeSuffix = @" $";
//        leftAxisFormatter.positiveSuffix = @" $";
        
        ChartYAxis *leftAxis = _chartView.leftAxis;
        leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
        leftAxis.labelCount = 8;
        leftAxis.valueFormatter = [[ChartDefaultAxisValueFormatter alloc] initWithFormatter:leftAxisFormatter];
        leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
        leftAxis.spaceTop = 0.15;
        leftAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
        
        ChartYAxis *rightAxis = _chartView.rightAxis;
        rightAxis.enabled = NO;
        rightAxis.drawGridLinesEnabled = NO;
        rightAxis.labelFont = [UIFont systemFontOfSize:10.f];
        rightAxis.labelCount = 8;
        rightAxis.valueFormatter = leftAxis.valueFormatter;
        rightAxis.spaceTop = 0.15;
        rightAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
        
        ChartLegend *l = _chartView.legend;
        l.horizontalAlignment = ChartLegendHorizontalAlignmentLeft;
        l.verticalAlignment = ChartLegendVerticalAlignmentBottom;
        l.orientation = ChartLegendOrientationHorizontal;
        l.drawInside = NO;
        l.form = ChartLegendFormSquare;
        l.formSize = 9.0;
        l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
        l.xEntrySpace = 4.0;
        
//        XYMarkerView *marker = [[XYMarkerView alloc]
//                                initWithColor: [UIColor colorWithWhite:180/255. alpha:1.0]
//                                font: [UIFont systemFontOfSize:12.0]
//                                textColor: UIColor.whiteColor
//                                insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0)
//                                xAxisValueFormatter: _chartView.xAxis.valueFormatter];
//        marker.chartView = _chartView;
//        marker.minimumSize = CGSizeMake(80.f, 40.f);
//        _chartView.marker = marker;

        
    }
    return _chartView;
}
- (UISegmentedControl *)segmentedControl
{
    if(!_segmentedControl)
    {
        UIView *superView = self;
        NSArray *segmentedArray = @[MyNSLocalizedString(@"周"),MyNSLocalizedString(@"月")];
        _segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedArray];
        _segmentedControl.selectedSegmentIndex = 0;
        _segmentedControl.tintColor = [UIColor colorWithHexString:@"F06161"];
        [_segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];
        [_segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"F06161"]} forState:UIControlStateNormal];
        [superView addSubview:_segmentedControl];
//        _segmentedControl
    }
    return _segmentedControl;
}
- (UIButton *)leftButton
{
    if(!_leftButton)
    {
        UIView *superView = self;
        _leftButton = [[UIButton alloc]init];
        [superView addSubview:_leftButton];
        [_leftButton setImage:imageWithName(@"left_btn") forState:UIControlStateNormal];
        _leftButton.contentMode = UIViewContentModeCenter;
    }
    return _leftButton;
}
- (UIButton *)rightButton
{
    if(!_rightButton)
    {
        UIView *superView = self;
        _rightButton = [[UIButton alloc]init];
        [superView addSubview:_rightButton];
        [_rightButton setImage:imageWithName(@"right_btn") forState:UIControlStateNormal];
        _rightButton.contentMode = UIViewContentModeCenter;
    }
    return _rightButton;
}
- (UILabel *)dateLabel
{
    if(!_dateLabel)
    {
        UIView *superView = self;
        _dateLabel = [[UILabel alloc]init];
        [superView addSubview:_dateLabel];
        [_dateLabel setFont:fontWithSize(15)];
        [_dateLabel setTextColor:[UIColor darkGrayColor]];
        _dateLabel.textAlignment = NSTextAlignmentCenter;
//        _dateLabel.text = <#text#>;
    }
    return _dateLabel;
}
- (UILabel *)radiateLabel
{
    if(!_radiateLabel)
    {
        UIView *superView = self.bottomView;
        _radiateLabel = [[UILabel alloc]init];
        [superView addSubview:_radiateLabel];
//        [_radiateLabel setFont:fontWithSize(15)];
        [_radiateLabel setTextColor:[UIColor colorWithHexString:@"F06161"]];
        _radiateLabel.textAlignment = NSTextAlignmentCenter;
//        _radiateLabel.text = <#text#>;
    }
    return _radiateLabel;
}
- (UILabel *)unitLabel
{
    if(!_unitLabel)
    {
        UIView *superView = self.bottomView;
        _unitLabel = [[UILabel alloc]init];
        [superView addSubview:_unitLabel];
        [_unitLabel setFont:fontWithSize(15)];
        [_unitLabel setTextColor:[UIColor darkGrayColor]];
        _unitLabel.textAlignment = NSTextAlignmentCenter;
//        _unitLabel.text = <#text#>;
    }
    return _unitLabel;
}

- (UIView *)bottomView
{
    if(!_bottomView)
    {
        UIView *superView = self;
        _bottomView = [[UIView alloc]init];
        [superView addSubview:_bottomView];
        _bottomView.backgroundColor = [UIColor colorWithHexString:@"FBFBFB"];
    }
    return _bottomView;
}

- (void)masLayoutSubview
{
    __weak UIView *superView = self;
    [self.segmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(82));
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.width.mas_equalTo(px1080Width(350));
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.top.mas_equalTo(self.segmentedControl.mas_bottom).with.offset(px1920Height(100));
    }];
    
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.dateLabel.mas_centerY);
        make.right.mas_equalTo(self.dateLabel.mas_left);
        make.height.width.mas_equalTo(px1920Height(130));
    }];
    
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.dateLabel.mas_centerY);
        make.left.mas_equalTo(self.dateLabel.mas_right);
        make.height.width.mas_equalTo(px1920Height(130));
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(superView);
        make.bottom.mas_equalTo(superView.mas_bottom).with.offset(px1920Height(-42));
        make.height.mas_equalTo(px1920Height(293));
    }];
    
    [self.radiateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.bottomView.mas_centerX);
        make.bottom.mas_equalTo(self.bottomView.mas_centerY).with.offset(px1920Height(-25));
    }];
    
    [self.unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.bottomView.mas_centerX);
        make.top.mas_equalTo(self.bottomView.mas_centerY).with.offset(px1920Height(25));
    }];
    
    [self.chartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.dateLabel.mas_bottom).with.offset(px1920Height(74));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(25));
        make.right.mas_equalTo(superView.mas_right).with.offset(px1080Width(-25));
        make.bottom.mas_equalTo(self.bottomView.mas_top).with.offset(px1920Height(-100));
//        make.height.mas_equalTo(px1920Height(800));

    }];
    
//    self.dateLabel.text = @"4月2日-4月9日";
    self.radiateLabel.text = @"0μT";
    self.unitLabel.text = MyNSLocalizedString(@"周辐射峰值");
    
    [self updateChartData];
}

@end
